package com.rong.mbt.personal.fragment;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rong.mbt.personal.R;
import com.rong.mbt.personal.activity.MonkeyPackageActivity;
import com.rong.mbt.personal.activity.MyCommunityActivity;
import com.rong.mbt.personal.activity.MyQRcodeActivity;
import com.rong.mbt.personal.activity.PersonalInfoActivity;
import com.rong.mbt.personal.bean.PersonalInfoBean;
import com.rong.mbt.personal.utils.ImageUtil;
import com.rong.mbt.personal.view.RoundImageView;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class NewPersonalFragment extends Fragment {

    private String mbtToken;
    private PersonalInfoBean infoBean;
    private RoundImageView ivHead;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvInvitaion;
    private SharedPreferences sp;



    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_personal, container, false);
        initView(view);
        listener(view);
        return view;
    }

    private void listener(View view) {
        //二维码
        view.findViewById(R.id.fnp_head_relative).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (infoBean == null || infoBean.getData() == null) {
                    return;
                }
                Intent intent = new Intent(getActivity(), MyQRcodeActivity.class);
                intent.putExtra("infoBean", infoBean);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });

        //设置
        view.findViewById(R.id.fnp_setting_linear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PersonalInfoActivity.class);
                if (infoBean != null) {
                    intent.putExtra("infoBean", infoBean);
                    intent.putExtra("mbtToken", mbtToken);
                }
                startActivity(intent);
            }
        });
        //钱包
        view.findViewById(R.id.fnp_monkey_pack_linear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MonkeyPackageActivity.class);
                if (infoBean != null) {
                    intent.putExtra("phone", infoBean.getData().getPhone());
                    intent.putExtra("mbtToken", mbtToken);
                    intent.putExtra("name", infoBean.getData().getName());
                }
                startActivity(intent);
            }
        });
        //好友邀请
        view.findViewById(R.id.fnp_nvitation_linear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (infoBean != null && infoBean.getData() != null && infoBean.getData().getBelongcode() != null) {
                    ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

                    cm.setText(infoBean.getData().getBelongcode());
                    Toast.makeText(getActivity(), "复制成功，赶紧邀请好友注册吧", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "复制失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        //我的矿机
        view.findViewById(R.id.ll_my_machine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build("/acitvity/MachineListActivity").navigation();
            }
        });

        //社区
        view.findViewById(R.id.fnp_shequ_linear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCommunityActivity.class);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);

            }
        });

        //好友分享
        view.findViewById(R.id.fnp_linear_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                ARouter.getInstance().build("/app/WXEntryActivity").withString("code",infoBean.getData().getBelongcode()).navigation();
                api = WXAPIFactory.createWXAPI(getActivity(), "wxb0065d1cb10fd744", true);
                api.registerApp("wxb0065d1cb10fd744");

                WXTextObject textObj = new WXTextObject();
                textObj.text = "盛大\n我下在使用盛大APP，新型社区APP，你也来试试吧！邀请码："+infoBean.getData().getBelongcode();

                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = textObj;
                msg.description = "盛大\n我下在使用盛大APP，新型社区APP，你也来试试吧！邀请码："+infoBean.getData().getBelongcode();

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = "text" + System.currentTimeMillis();
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneSession;
                api.sendReq(req);
            }
        });
    }
    private IWXAPI api;


    @Override
    public void onStart() {
        super.onStart();
        initNet();
    }

    private void initNet() {
        OkGo.<String>get("http://service.kantingyong.com/user/getUser")
                .tag(this)
                .headers("mbtToken", mbtToken)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (!response.body().isEmpty()) {
                            infoBean = JSON.parseObject(response.body(), PersonalInfoBean.class);
                            if (infoBean == null || infoBean.getData() == null) {
                                return;
                            }
                            if (infoBean.getData().getHeadimage() != null) {
                                ImageUtil.loadRoundeImga(getActivity(), infoBean.getData().getHeadimage(), ivHead, 10);
                            }

                            tvName.setText(infoBean.getData().getName());
                            tvPhone.setText("盛大账号：" + infoBean.getData().getPhone());
                            tvInvitaion.setText("好友邀请 (" + infoBean.getData().getBelongcode() + ")");
                            SharedPreferences.Editor editor = sp.edit().putString("belongcode", infoBean.getData().getBelongcode());
                            editor.apply();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.i("test", "onError  " + response.body());
                    }
                });
    }

    private void initView(View view) {
        sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
        mbtToken = sp.getString("mbtToken", "");
        view.findViewById(R.id.head_blue_iv_left).setVisibility(View.GONE);
        TextView tvTitle = view.findViewById(R.id.head_blue_tv_center);
        tvTitle.setText("个人中心");
        view.findViewById(R.id.head_blue_iv_right).setVisibility(View.GONE);

        ivHead = view.findViewById(R.id.pf_np_iv_head);
        tvName = view.findViewById(R.id.pf_np_tv_name);
        tvPhone = view.findViewById(R.id.pf_np_tv_username);

        tvInvitaion = view.findViewById(R.id.fnp_monkey_pack_tv_invitation);
    }
}
